<template>
  <el-dialog
    title="新增员工"
    :visible="isShow"
    width="500px"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="form.username" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="手机:" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="入职时间:" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" placeholder="选择日期" />
      </el-form-item>
      <el-form-item label="聘用形势:" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请输入">
          <el-option
            v-for="item in employees.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号:" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="部门:" prop="departmentName">
        <div ref="treeArea">
          <el-input
            v-model="form.departmentName"
            readonly
            placeholder="请输入"
            @focus="focusEvent"
          />
          <el-tree
            v-if="showTree"
            :data="treeData"
            :props="{ label: 'name' }"
            @node-click="nodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item
        label="转正时间:"
        prop="correctionTime"
        placeholder="选择日期"
      >
        <el-date-picker v-model="form.correctionTime" type="date" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="footer">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="add">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import employees from '@/assets/constant/employees.js'
import { companyDepartment } from '@/api/departments.js'
import { SysUserpost } from '@/api/employees.js'
export default {
  name: 'Dialog',
  props: {
    isShow: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {
      showTree: false,
      treeData: [],
      employees: employees,
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	非必须		入职时间
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		姓名
        mobile: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		手机号
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ], //	number	非必须		聘用形式
        workNumber: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		工号
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ], //	string	非必须		组织名称
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }], //	非必须		入职时间
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }] //	string	非必须		转正时间
      }
    }
  },
  mounted() {
    document.addEventListener('click', this.clickEvent)
  },

  methods: {
    closeEvent() {
      this.$emit('update:isShow', false)
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await SysUserpost(this.form)
          this.$message.success('新增成功')
          this.closeEvent()
          this.$emit('getData')
          console.log(result)
        }
      })
    },
    add() {
      this.isShow = false
    },
    async focusEvent() {
      if (this.treeData.length === 0) {
        this.showTree = true
        const res = await companyDepartment()
        this.treeData = this.changeData(res.data.depts)
        console.log(res)
      }
    },
    changeData(arr, str = '') {
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    nodeClick(data) {
      this.form.departmentName = data.name
      console.log(data)
    },
    clickEvent(e) {
      if (this.isShow && !this.$refs.treeArea.contains(e.target)) {
        this.showTree = false
      }
    },
    beforeDestroy() {
      document.removeEventListener('click', this.clickEvent)
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  text-align: center;
}
</style>
